package com.example.qianfeng.image.stream;

import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;

public class ImageFormatChecker {
    /**
     * 判断输入流是什么格式的文件
     *
     * @param inputStream inputStream
     * @return String
     * @throws IOException IOException
     */
    public static String getImageFormat(FileInputStream inputStream) throws IOException {
        byte[] buffer = new byte[8];
        inputStream.read(buffer);

        if (buffer[0] == (byte) 0xFF && buffer[1] == (byte) 0xD8) {
            return ".jpg";
        } else if (buffer[0] == (byte) 0x89 && buffer[1] == (byte) 0x50 && buffer[2] == (byte) 0x4E && buffer[3] == (byte) 0x47
                && buffer[4] == (byte) 0x0D && buffer[5] == (byte) 0x0A && buffer[6] == (byte) 0x1A && buffer[7] == (byte) 0x0A) {
            return ".png";
        } else if (buffer[0] == (byte) 0x47 && buffer[1] == (byte) 0x49 && buffer[2] == (byte) 0x46 && buffer[3] == (byte) 0x38) {
            return ".gif";
        } else if (buffer[0] == (byte) 0x42 && buffer[1] == (byte) 0x4D) {
            return ".bmp";
        } else if (buffer[0] == (byte) 0x49 && buffer[1] == (byte) 0x49 && buffer[2] == (byte) 0x2A && buffer[3] == (byte) 0x00) {
            return ".tiff";
        } else {
            return "Unknown";
        }
    }

    /**
     * 把上传图片的时间字符串转换成时间戳
     *
     * @param dateString 时间字符串
     * @return long
     */
    public static long getTimeStampSys(String dateString) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        long timestamp = 0;
        if (!dateString.equals("temp")){
            try {
                Date date = sdf.parse(dateString);
                timestamp = date.getTime();
                return timestamp;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return 0;
    }

    /**
     * 初始化时间
     *
     * @param calendar calendar
     * @return calendar
     */
    public static Calendar initCalendar(Calendar calendar) {
        int month = calendar.get(Calendar.MONTH) + 1;
        //int month = LocalDate.now().getMonthValue() - 1; // 获取上传时间的月份
        if (month == 2) {
            int year = LocalDate.now().getYear(); // 获取年份
            if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { // 闰年
                calendar.set(Calendar.DAY_OF_YEAR,calendar.get(Calendar.DAY_OF_YEAR)+29);
            }else { // 平年
                calendar.set(Calendar.DAY_OF_YEAR,calendar.get(Calendar.DAY_OF_YEAR)+28);
            }
            return calendar;
        }
        switch (month){
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                calendar.set(Calendar.DAY_OF_YEAR,calendar.get(Calendar.DAY_OF_YEAR)+31);
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                calendar.set(Calendar.DAY_OF_YEAR,calendar.get(Calendar.DAY_OF_YEAR)+30);
                break;
            default:
                break;
        }
        return calendar;
    }
}
